package com.ymatou.iapi.productmanager.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.parameter.GetSellerProductListBean;
import com.ymatou.iapi.productmanager.parameter.SellerProductDto;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.service.GetSellerProductListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
/**
*获取买手商品列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetSellerProductList{
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productsellerid"));
	private static GetSellerProductListBean getsellerproductlistBean = new GetSellerProductListBean();
	private static GetSellerProductListCall getsellerproductlistCall = new GetSellerProductListCall();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取买手商品列表");
	}

	@Before
	public void caseUp() {
		getsellerproductlistBean=new GetSellerProductListBean();
		getsellerproductlistCall=new GetSellerProductListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getsellerproductlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerProductList_001() {
		Logger.start(true,"获取买手商品列表,商品列表为空");
		try {
			getsellerproductlistBean.setSellerId(sellerId);
			getsellerproductlistBean.setPageIndex(1);
			getsellerproductlistBean.setPageSize(2);
			getsellerproductlistCall.setData(getsellerproductlistBean);
			JSONObject ret = getsellerproductlistCall.callAndGetReturnData(getsellerproductlistBean);			
			Logger.verifyEquals("200", ret.getString("Code"), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerProductList_002() {
		Logger.start(true,"获取买手商品列表");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			//创建mock商品
			String productId1 =ProductManagerCallServiceV3.AddSellerProduct(addProdReq);
			String productId2 =ProductManagerCallServiceV3.AddSellerProduct(addProdReq);
			//String productId1 = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//String productId2 = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			
			//手动调同步mongo
			ProductManagerCallServiceV3.createProduct_sync(productId1);
			//手动调同步mongo
			ProductManagerCallServiceV3.createProduct_sync(productId2);
			
			List<String> productIdList = new ArrayList<String>();
			productIdList.add(productId1);
			productIdList.add(productId2);
			getsellerproductlistBean.setProductIdList(productIdList);
			getsellerproductlistBean.setSellerId(sellerId);
			getsellerproductlistBean.setPageIndex(1);
			getsellerproductlistBean.setPageSize(2);
			getsellerproductlistCall.setData(getsellerproductlistBean);
			getsellerproductlistCall.callService();			
			checkGetSellerProductList(getsellerproductlistCall,productIdList);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerProductList_003() {
		Logger.start(true,"获取买手商品列表,不是创建商品的买手");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			//创建mock商品
			String productId1 =ProductManagerCallServiceV3.AddSellerProduct(addProdReq);
			String productId2 =ProductManagerCallServiceV3.AddSellerProduct(addProdReq);
			//手动调同步mongo
			ProductManagerCallServiceV3.createProduct_sync(productId1);
			//手动调同步mongo
			ProductManagerCallServiceV3.createProduct_sync(productId2);
			
			//String productId1 = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//String productId2 = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			List<String> productIdList = new ArrayList<String>();
			productIdList.add(productId1);
			productIdList.add(productId2);
			getsellerproductlistBean.setProductIdList(productIdList);
			getsellerproductlistBean.setSellerId(3383);
			getsellerproductlistBean.setPageIndex(1);
			getsellerproductlistBean.setPageSize(2);
			getsellerproductlistCall.setData(getsellerproductlistBean);
			JSONObject ret = getsellerproductlistCall.callAndGetReturnData(getsellerproductlistBean);
			//商品为空
			Logger.verifyEquals(true, ret.getJSONObject("Data").getJSONArray("ProductList").length()==0, "商品为空");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	
	
	public static void checkGetSellerProductList(GetSellerProductListCall getsellerproductlistCall,List<String> productIdList) throws InterruptedException{
		List<SellerProductDto> productList = getsellerproductlistCall.getProductList();
		for(int i = 0;i < productList.size();i++){
			SellerProductDto sellerProductDto = productList.get(i);
			Logger.debug(sellerProductDto);
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productIdList.get(i)).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			
			IntergratedProductMapper ipm = SqlMapperFactory.getIntergratedProductMapper();
			Map<String,Object> productInMap = ipm.selectProductByProdId(productIdList.get(i));
			Logger.verifyEquals(sellerProductDto.getProductId(), productInMap.get("sProductId").toString(), "验证ProductId");
			Logger.verifyEquals(sellerProductDto.getProductName(), productInMap.get("sProduct").toString(), "验证ProductName");
			Logger.verifyEquals(sellerProductDto.getThirdCategoryId(), (Integer)productInMap.get("iThirdCategoryId"), "验证ThirdCategoryId");
			Logger.verifyEquals(sellerProductDto.getBrandId().toString(), productInMap.get("iBrandId").toString(), "验证BrandId");
			Logger.verifyEquals(sellerProductDto.getBrandName().replaceAll("[^x00-xff]*", ""), productInMap.get("sBrand").toString(), "验证BrandName");
			Logger.verifyEquals(sellerProductDto.getCatalogStatus().toString(),productInMap.get("iCatalogStatus").toString(), "验证CatalogStatus");
			YmtCatalogsI Catalog = new YmtCatalogsIWapper().selectCatalogsByCatalogId(catalogs.get(i));
			Logger.verifyEquals(sellerProductDto.getSkuList().get(i).getCatalogId(), Catalog.getsCatalogId(), "验证CatalogId");
			Logger.verifyEquals(sellerProductDto.getSkuList().get(i).getQuotePrice(), Catalog.getfQuotePrice().setScale(1).doubleValue(), "验证QuotePrice");
			Logger.verifyEquals(sellerProductDto.getSkuList().get(i).getVipPrice(), Catalog.getfVipPrice().setScale(1).doubleValue(), "验证VipPrice");
			Logger.verifyEquals(sellerProductDto.getSkuList().get(i).getStockNum(), Catalog.getiNum(), "验证StockNum ");
			
			Date addTime = YMTDateUtil.getDate(sellerProductDto.getAddTime(),YMTDateUtil.YMDHMS);
			Date updateTime = YMTDateUtil.getDate(sellerProductDto.getUpdateTime(),YMTDateUtil.YMDHMS);
			Date AddTime = (Date)productInMap.get("dAddTime");
			Date UpdateTime = (Date)productInMap.get("dLastUpdate");
			
			Date startTime = YMTDateUtil.getDate(sellerProductDto.getStartTime(),YMTDateUtil.YMDHMS);
			Date endTime = YMTDateUtil.getDate(sellerProductDto.getEndTime(),YMTDateUtil.YMDHMS);
			Date validStart = (Date)productInMap.get("validStart");
			Date validEnd = (Date)productInMap.get("validEnd");
			
			AssertService.assertTime(addTime, AddTime, 120,"dAddTime");
			AssertService.assertTime(updateTime, UpdateTime,120, "dLastUpdate");
			AssertService.assertTime(startTime, validStart, 120,"validStart");
			AssertService.assertTime(endTime, validEnd,120, "validEnd");
		}
	}
	
	
	////////////////////忽略case///////////////////
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_GetSellerProductList_004() {
		Logger.start(true,"获取买手商品列表,获取新版物流方式(全球优先业务下架)");
		try {
			AddSellerProductRequest addProdReq =ProductManagerCallServiceV2.initAddSellerGlobalPspProductRequest(3383);
			addProdReq.getProduct().setCatalogStatus(10);
			addProdReq.getProduct().setDeliveryCompanyType(1);
			addProdReq.getProduct().setDeliveryAging(5);
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Thread.sleep(3000);
			getsellerproductlistBean.setProductIdList(Arrays.asList(productId));
			getsellerproductlistBean.setSellerId(3383);
			getsellerproductlistBean.setPageIndex(1);
			getsellerproductlistBean.setPageSize(2);
			getsellerproductlistCall.setData(getsellerproductlistBean);
			JSONObject ret = getsellerproductlistCall.callAndGetReturnData(getsellerproductlistBean);
			JSONObject productInfo = ret.getJSONObject("Data").getJSONArray("ProductList").getJSONObject(0);
			Logger.verifyEquals(10, productInfo.getInt("CatalogStatus"), "验证catalogstatus变成新类型");
			Logger.verifyEquals(1, productInfo.getInt("DeliveryCompanyType"), "验证物流公司");
			Logger.verifyEquals(5, productInfo.getInt("DeliveryAging"), "验证物流时效");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}